package servlet;


import bean.Grade;
import bean.Student;
import service.GradeService;
import service.StudentService;
import service.impl.GradeServiceImpl;
import service.impl.StudentServiceImpl;
import util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(urlPatterns = "/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");

        if ("insert".equals(method)){
            insertStu(req,resp);
        }else if ("update".equals(method)){
            updateStu(req,resp);
        }else if ("findbyid".equals(method)){
            findById(req,resp);
        }else if ("delete".equals(method)){
            deleteStu(req,resp);
        } else {
            findList(req,resp);
        }

    }


    /**
     * 新增学生信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void insertStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gradeId = req.getParameter("gradeId");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introdction = req.getParameter("introdction");

        //调取service

        StudentService service = new StudentServiceImpl();

        //将参数封装到学生对象中
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gradeId));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introdction);

        //获取数据入库时的影响行数
        int i = service.insertStu(student);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();


        if (i>0){
            writer.println("<script>alert('新增成功~');location.href='/Educational/student/studentServlet'</script>");

        }else {
            writer.println("<script>alert('新增失败!');location.href='/Educational/student/getGradeList'</script>");
        }

    }


    /**
     * 删除学生信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sid = req.getParameter("sid");

        //根据主键查询学生信息
        StudentService service = new StudentServiceImpl();

        int i = service.delStu(sid);

        resp.setContentType("text/html;charset=utf-8");

        PrintWriter writer = resp.getWriter();

        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet'</script>");
        }

    }

    /**
     * 更新学生信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sid = req.getParameter("sid");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        Student stu = new Student();

        stu.setStuNo(stuNo);
        stu.setStuName(stuName);
        stu.setAddress(address);
        stu.setSex(Integer.parseInt(sex));
        stu.setGid(Integer.parseInt(gid));
        stu.setEmail(email);
        stu.setRegistered(registered);
        stu.setPhone(phone);
        stu.setPolitics(politics);
        stu.setIdNumber(idNumber);
        stu.setProfession(profession);
        stu.setIntroduction(introduction);
        stu.setStuId(Integer.parseInt(sid));

        StudentService service = new StudentServiceImpl();
        int i = service.updateStu(stu);

        resp.setContentType("text/html;charset=utf-8");

        PrintWriter writer = resp.getWriter();

        if(i>0){
            writer.println("<script>alert('更新成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('更新失败');location.href='/Educational/student/studentServlet?method=findbyid?sid="+sid+"'</script>");
        }

    }


    /**
     * 查询学生信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取参数
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");

        //分页数据  limt 开始位置，显示条数
        //当前页码值
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageIndex的值，则认为默认查询第一页
        int index = (pageIndex==null)?1:Integer.parseInt(pageIndex);


        //调取service方法
        StudentService service = new StudentServiceImpl();
        //分页工具类
        PageUtil pageUtil = new PageUtil();

        //设置性别默认值为-1，即未选择性别
        int usex = (sex==null||sex.length()==0)?-1:Integer.parseInt(sex);

        //获取数据
        List<Student> students = service.getStudents(stuname,stuno,usex,index, pageUtil.getPageSize());

        //获取总页数=总条数%每页显示的条数>0?(总条数/每页显示条数)+1:(总条数/每页显示条数)
        int total = service.total(stuname,stuno,usex);  //总条数

        //将分页数据，学生信息数据存到分页工具类对象中
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(index);
        pageUtil.setDataList(students);


        //后台要给前台传数据时，一定要先存值，并跳转页面
        //向页面存储分页数据和学生数据
        req.setAttribute("p1",pageUtil);


        //模糊查询条件
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);


        //转发方式跳转页面
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }


    /**
     * 主键查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String sid = req.getParameter("sid");

        //根据主键查询学生信息
        StudentService service = new StudentServiceImpl();
        Student student = service.findById(Integer.parseInt(sid));

        //查询年级列表
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> list = gradeService.getList();

        //存值
        req.setAttribute("glist",list);
        req.setAttribute("stu",student);

        //转发跳转页面
        req.getRequestDispatcher("edit.jsp").forward(req,resp);

    }
}
